﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AgvDemo
{
    public partial class LoginForm : Form
    {
        List<AgvInfoExport> agvInfoExports = new List<AgvInfoExport>();

        public LoginForm(List<ErrorClass> errorTemp)
        {
            InitializeComponent();




            dgTask.EnableHeadersVisualStyles = false;//这一句很重要，否则下面的列头设置不起作用
            //dgError.EnableHeadersVisualStyles = false;//这一句很重要，否则下面的列头设置不起作用
            //dgAgvInfo.EnableHeadersVisualStyles = false;//这一句很重要，否则下面的列头设置不起作用
            dgTask.AllowUserToAddRows = false;

            try
            {
                if (errorTemp.Count > 0)
                {
                    dgTask.Rows.Clear();
                    agvInfoExports.Clear();

                    for (int i = 0; i < errorTemp.Count; i++)
                    {
                        int index = dgTask.Rows.Add();
                        dgTask.Rows[index].Cells[0].Value = errorTemp[i].ErrorAgv;

                        dgTask.Rows[index].Cells[1].Value = errorTemp[i].ErrorStation;
                        switch (errorTemp[i].ErrorCode)
                        {
                            case 1: dgTask.Rows[index].Cells[2].Value = "左驱动器异常"; break;
                            case 2: dgTask.Rows[index].Cells[2].Value = "电源电量过低"; break;
                            case 3: dgTask.Rows[index].Cells[2].Value = "机械防撞生效"; break;
                            case 4: dgTask.Rows[index].Cells[2].Value = "右驱动器异常"; break;
                            case 5: dgTask.Rows[index].Cells[2].Value = "牵引升降异常"; break;
                            case 6: dgTask.Rows[index].Cells[2].Value = "驱动升降异常"; break;
                            case 7: dgTask.Rows[index].Cells[2].Value = "急停按下"; break;
                            case 8: dgTask.Rows[index].Cells[2].Value = "脱轨异常"; break;
                          
                            //default: dgTask.Rows[index].Cells[0].Value = "未知异常"; break;
                        }

                        dgTask.Rows[index].Cells[3].Value = errorTemp[i].ErrorDate;



                        switch (errorTemp[i].ErrorCode)
                        {
                            case 1: agvInfoExports.Add(new AgvInfoExport()
                            {
                                agvNum = errorTemp[i].ErrorAgv,
                                faultStation=errorTemp[i].ErrorStation,
                                faultDes = "左驱动器异常",
                                ctime = errorTemp[i].ErrorDate,

                            }); break;
                            case 2: agvInfoExports.Add(new AgvInfoExport()
                            {
                                agvNum = errorTemp[i].ErrorAgv,
                                faultStation = errorTemp[i].ErrorStation,
                                faultDes = "电源电量过低",
                                ctime = errorTemp[i].ErrorDate,

                            }); break;
                            case 3: agvInfoExports.Add(new AgvInfoExport()
                            {
                                agvNum = errorTemp[i].ErrorAgv,
                                faultStation = errorTemp[i].ErrorStation,
                                faultDes = "机械防撞生效",
                                ctime = errorTemp[i].ErrorDate,

                            }); break;
                            case 4: agvInfoExports.Add(new AgvInfoExport()
                            {
                                agvNum = errorTemp[i].ErrorAgv,
                                faultStation = errorTemp[i].ErrorStation,
                                faultDes = "右驱动器异常",
                                ctime = errorTemp[i].ErrorDate,

                            }); break;
                            case 5: agvInfoExports.Add(new AgvInfoExport()
                            {
                                agvNum = errorTemp[i].ErrorAgv,
                                faultStation = errorTemp[i].ErrorStation,
                                faultDes = "牵引升降异常",
                                ctime = errorTemp[i].ErrorDate,

                            }); break;
                            case 6: agvInfoExports.Add(new AgvInfoExport()
                            {
                                agvNum = errorTemp[i].ErrorAgv,
                                faultStation = errorTemp[i].ErrorStation,
                                faultDes = "驱动升降异常",
                                ctime = errorTemp[i].ErrorDate,

                            }); break;
                            case 7: agvInfoExports.Add(new AgvInfoExport()
                            {
                                agvNum = errorTemp[i].ErrorAgv,
                                faultStation = errorTemp[i].ErrorStation,
                                faultDes = "急停按下",
                                ctime = errorTemp[i].ErrorDate,

                            }); break;
                            case 8: agvInfoExports.Add(new AgvInfoExport()
                            {
                                agvNum = errorTemp[i].ErrorAgv,
                                faultStation = errorTemp[i].ErrorStation,
                                faultDes = "脱轨异常",
                                ctime = errorTemp[i].ErrorDate,

                            }); break;
                          
                            //default: dgTask.Rows[index].Cells[0].Value = "未知异常"; break;
                        }

                    
                    }

                }
            }
            catch
            { }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dr = MessageBox.Show("是否当前数据生成EXCEL报表？？？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                if (dr == DialogResult.OK)
                {
                    if (dgTask.Rows.Count != 0)
                    {
                        SaveFileDialog dialog = new SaveFileDialog();
                        dialog.Filter = "Excel表格(*.xls)|*.xls";
                        if (dialog.ShowDialog() == DialogResult.OK)
                        {
                            NpoiExport<AgvInfoExport>.SaveXlsChangeds(dialog.FileName, agvInfoExports);
                        }
                    }
                    else
                    {
                        MessageBox.Show("无数据可导出！！！");
                    }
                }
            }
            catch
            {

            }
        }
   
    }


    public class AgvInfoExport
    {
        [Description("AGV编号")]
        public int agvNum { get; set; }
        [Description("故障站点")]
        public int faultStation { get; set; }
        [Description("故障内容")]
        public string faultDes { get; set; }
        [Description("发生时间")]
        public DateTime ctime { get; set; }
     
        //[Description("停留时间(s)")]
        //public string StayTime { get; set; }
        //[Description("停留原因")]
        //public string StayReason { get; set; }
    }
}
